package com.system.presideover.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.common.result.Result;
import com.system.presideover.entity.AppletUser;
import com.system.presideover.service.AppletUserService;
import com.system.presideover.utils.HttpClients;
import com.system.presideover.utils.JwtUtils;
import com.system.presideover.utils.LoginDto;
import com.system.presideover.utils.WXPhoneLogin;
import com.system.vo.AppletVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 微信程序用户 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-04-16
 */
@Api(tags = "微信程序用户")
@RestController
@RequestMapping("/presideover/applet-user")
public class AppletUserController {


    @Autowired
    private AppletUserService appletUserService;

    @Autowired
    private AppletVO applet;

    @Autowired
     private HttpClients httpClientUtil;

    @Autowired
    private WXPhoneLogin wxPhoneLogin;

    @ApiOperation("获取头像和昵称")
    @PostMapping("/getHeadAndName/{code}")
    public Result getHeadAndName(@PathVariable String code) {
        //判断客户端传的code是否为空
        if (code == null) {
            return Result.fail().message("code不能为空");
        }
        LoginDto loginDto = wxPhoneLogin.appWxLogin(code);
        QueryWrapper<AppletUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", loginDto.getOpenid());
        AppletUser appletUser1 = appletUserService.getOne(queryWrapper);
        if (appletUser1 == null) {

            AppletUser appletUser = new AppletUser();
            String randomCharacters = AppletUserController.getRandomCharacters(6);
            appletUser.setNickName(randomCharacters);
            System.out.println("生成名称 = " + randomCharacters);
            appletUser.setOpenid(loginDto.getOpenid());
            appletUser.setCreateTime(LocalDateTime.now());
            boolean save = appletUserService.save(appletUser);

            Map<String, Object> cliams = new HashMap<>();
            cliams.put("id",appletUser.getId());
            String token = JwtUtils.generateJwt(cliams);
            return Result.ok(token).message("登陆成功");
        } else {
            appletUser1.setUpdateTime(LocalDateTime.now());//
            appletUserService.updateById(appletUser1);
            //生成token
            Map<String, Object> cliams = new HashMap<>();
            cliams.put("id",appletUser1.getId());
            String token = JwtUtils.generateJwt(cliams);
            return Result.ok(token).message("登陆成功");

        }

    }


    public static String getRandomCharacters(int length) {

        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

}

